$(document).ready(function() {
    if($('#search_keyword').val != ''){
        $('.delQuery').css('display','block');
    }
    var cat_choosed = document.getElementsByClassName("cat-active");
    if (cat_choosed.length >= 1) {
        var cat_name = cat_choosed[cat_choosed.length -1].textContent;
        var cat_result = cat_name.replace(/[0-9\.]+$/g, '').trim();
        $("#searchBarcat").text(cat_result);
        $("#searchBarcat").html(cat_result + ' <i class="fa fa-caret-down fa-lg"></i>');
    }

    $('.tTip').tooltip();
    
    //offcanvas Slidebar left
    $('[data-toggle=offcanvas]').click(function() {
        $('.row-offcanvas').toggleClass('active');
    });

    //readMorePrInfo
    $('.readMorePrInfo').click(function() {
        $('.prInfoContent').toggleClass("expand");
    });
    
    $("#search_keyword").click(function(event) {
        event.stopPropagation();
        $(".frmSearch").addClass("active");
    });

    $(".nav-main").click(function(event) {
        event.stopPropagation();
        $(this).children(".textSub").addClass('active');
        category_nav_hide('show');
        category_select_search('hide');
    });
    $('.category-home-show').click(function() {
        var elem = $(this);
        elem.addClass('active');
        category_nav_hide();
        category_select_search('show');
    });
    $('.list-searAll a').click(function() {
        var elem = $(this);
        var value = elem.attr('rel');
        var cat_name = elem.html() + ' <i class="fa fa-caret-down fa-lg"></i>';
        var hidden_cat = '<input type="hidden" value="' + value + '" name="c">';
        $('#category_hiden').html(hidden_cat);
        $('.category-home-show').html(cat_name);
        category_select_search('hide');
        $('#search_keyword').focus();
        return false;
    });
    $('html').click(function() {
        category_nav_hide();
        category_select_search();
    });
    $('.list-searAll, .category-home-show').click(function(event) {
        event.stopPropagation();
    });
    $('#search_keyword').typeahead({
            highlight:true,
            minLength:3
    },
    {
    source: function(query,process) {
        var url = $('#search_keyword').attr('suggest');
        return  $.ajax({
                        url:url, 
                        data:{ query: query,limit:8 }, 
                        dataType: 'JSON', 
                        success: function (data) {
                            process(data.suggestion);
                        }
                });

    },
    displayKey: 'query',
    templates: {
        suggestion: Handlebars.compile([
        '<p class="sg-category">{{category}}</p>',
        '<p class="sg-keyword">{{query}}</p>',
        '<p class="sg-price">{{price}}</p>'
        ].join(''))
    },

    }).on('typeahead:selected', function (obj, data) {
        window.location.href=$('#site_url').val()+"s?q="+data.query;
    }).keyup(function(){
        $('.delQuery').css('display','block');
        if(!this.value){
            $('.delQuery').css('display','none');
        }
    }).focus();

    $('.delQuery').on('click',function(){
        $('#search_keyword').val('');
        $('.delQuery').css('display','none');
        $('#search_keyword').focus();
    });
});

var date = new Date();
var hour = date.getUTCHours();
var minute = date.getUTCMinutes();
var second = date.getUTCSeconds();

var counter = 2000000 + hour*10000 + minute*100 + second;
setInterval ("timer()", 1300);

function timer(){
    var rand = Math.floor(Math.random() * 3);
    counter += rand;
    $("#prdCounter").text(commaSeparateNumber(counter));
}
function commaSeparateNumber(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
        val = val.toString().replace(/(\d+)(\d{3})/, '$1'+'.'+'$2');
    }
    return val;
}